import { ElMessage, ElMessageBox } from "element-plus";
import {
  recordList,
  recordAdd,
  exportDataToExcel,
  addDiseaseData,
  recordDetail,
  updatePeopleFacialPhoto,
} from "@/api/record.js";
export const recordMixin = {
  data() {
    return {
      fileList: [],
      DialogFacialCapture: false,
      DialogHomeVisit: false,
      DialogAddPersonRecord: false,
      submitParams: {},
      Record: {},
      newperson: {},
      rules: {
        idcard: [
          {
            required: true,
            message: "请输入18位身份证号",
            pattern:
              /^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\d|30|31)\d{3}[\dXx]$/,
            trigger: "change",
          },
        ],
        name: [
          {
            required: true,
            message: "请输入姓名",
            trigger: "blur",
          },
        ],
        maritalrelation: [
          {
            required: true,
            message: "请选择婚姻状态",
            trigger: "blur",
          },
        ],
        telephone: [
          {
            required: true,
            message: "请输入正确的手机号",
            pattern: /^(?:(?:\+|00)86)?1[3-9]\d{9}$/,
            trigger: "blur",
          },
        ],
        address: [
          {
            required: true,
            message: "请输入详细住址",
            trigger: "blur",
          },
        ],
        sex: [
          {
            required: true,
            message: "请选择性别",
            trigger: "blur",
          },
        ],
        age: [
          {
            required: true,
            message: "请输入年龄",
            trigger: "blur",
          },
        ],
        volk: [
          {
            required: true,
            message: "请输入民族",
            trigger: "blur",
          },
        ],
        village: [
          {
            required: true,
            message: "请选择村(居)委会",
            trigger: "blur",
          },
        ],
        disease: [
          {
            required: true,
            message: "请输入患者疾病",
            trigger: "blur",
          },
        ],
        result: [
          {
            required: true,
            message: "请输入本次家访结果",
            trigger: "blur",
          },
        ],
        lastResult: [
          {
            required: true,
            message: "请选择本次家访状况",
            trigger: "blur",
          },
        ],
        nextTime: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
      },
      form: {
        name: "",
        idcard: "",
      },
    };
  },
  methods: {
    handleFileChange(file, fileList) {
      this.fileList = fileList;
    },
    openFacialCapture() {
      this.DialogFacialCapture = true;
    },

    saveFacialPhoto() {
      this.$refs.Record.validate((valid) => {
        if (valid) {
          let lag = false;
          let formData = new FormData();
          this.fileList.forEach((item) => {
            formData.append("file", item.raw);
            lag = true;
          });
          formData.append("idcard", this.Record.idcard);
          if (lag == true) {
            updatePeopleFacialPhoto(formData).then((res) => {
              if (res.data.code == 200) {
                ElMessage({
                  message: "保存成功",
                  type: "success",
                });
              } else {
                ElMessage.error("保存失败");
              }
              this.Record = {};
              this.fileList = [];
              this.$refs.uploadComp.clearFiles();
              this.closeDialog();
              this.fetchRecord();
            });
          } else {
            ElMessage.error("请选择照片");
          }
        }
      });
    },
    fetchRecord() {
      // this.loading=true
      recordList().then((res) => {
        res.data.data.forEach((item) => {
          let str = `data:image/jpg;base64,${item.photo}`; //获取数据
          item.photo = str.replace(/[\r\n]/g, ""); //去除空格，否则会导致图片碎开
        });
        this.PersonalRecords = res.data.data;
        // console.log(res.data.data);
        // this.loading=false
      });
    },
    SelectPerson() {
      console.log(this.form, "sss");
      if (this.form.name == "" && this.form.idcard == "") {
        this.fetchRecord();
      } else {
        recordDetail(this.form).then((res) => {
          res.data.data.forEach((item) => {
            let str = `data:image/jpg;base64,${item.photo}`; //获取数据
            item.photo = str.replace(/[\r\n]/g, ""); //去除空格，否则会导致图片碎开
          });
          this.PersonalRecords = res.data.data;
          this.FamilyRecords = res.data.data;
          this.form = {};
        });
      }
    },
    EstablishingFamily: function () {
      this.$router.push(`/record/personal/EsFamily`);
    },
    resetForm() {
      this.$refs["newperson"].resetFields();
      this.DialogAddPersonRecord = false;
    },
    AddPersonRecord() {
      this.$refs.newperson.validate((valid) => {
        console.log(valid);
        if (valid) {
          console.log("submit!,校验通过");
          console.log(this.newperson);
          ElMessageBox.confirm("确定添加此成员档案?", "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "关闭",
            type: "warning",
          })
            .then(() => {
              ElMessage({
                type: "success",
                message: "添加成功",
              });
              // this.form.date = this.form.date.toLocaleDateString().replace(/\//g, "/")
              this.DialogAddPersonRecord = false;
              return recordAdd(this.newperson);
              // console.log(parseInt('2021-10-11'.replace(/\-/g, "")));
            })
            .then(() => {
              this.resetForm();
              this.fetchRecord();
            })
            .catch(() => {
              ElMessage({
                type: "info",
                message: "取消发布",
              });
              this.resetForm();
            });
        }
      });
    },
    closeDialog() {
      this.DialogHomeVisit = false;
      this.DialogFamilyEdit = false;
      this.DialogFacialCapture = false;
    },
    addDisease() {
      this.DialogHomeVisit = true;
    },
    saveHomeForm() {
      addDiseaseData(this.submitParams).then((res) => {
        if (res.data.code == 200) {
          this.$notify({
            title: "成功",
            message: "患者" + this.submitParams.name + "疾病记录成功",
            type: "success",
            duration: 1500,
          });
        }
      });
      this.DialogHomeVisit = false;
    },
    ExportDataExcel() {
      exportDataToExcel("").then((res) => {
        console.log(res.data.data);
      });
    },
  },
};
